﻿#pragma once

#ifndef QCEFWINMSGLOOP_H
#define QCEFWINMSGLOOP_H

#include <Windows.h>
#include <CommCtrl.h>

#include "QCefHeader.h"
#include "QCefDebug.h"

class QCefWinMsgLoop
{
private:
    static LRESULT CALLBACK WndProc(HWND hwnd,
                                    UINT msg,
                                    WPARAM wparam,
                                    LPARAM lparam);

public:
    inline static QCefWinMsgLoop * instance() { return ms_instance; }

protected:
    static QCefWinMsgLoop * ms_instance;         //

public:
    QCefWinMsgLoop();
    ~QCefWinMsgLoop();

public:
    void Quit();
    void Run();
    void OnScheduleMessagePumpWork(UINT delay_ms);

    // The platform subclass calls this method on the main application thread in
    // response to the OnScheduleMessagePumpWork() call.
    void OnScheduleWork(UINT delay_ms);

    // The platform subclass calls this method on the main application thread when
    // the pending work timer times out.
    void OnTimerTimeout();

    // Handle work processing.
    void DoWork();

    bool PerformMessageLoopWork();

protected:
    void setTimer(UINT delay_ms);
    void killTimer();
    bool isSetTimer() { return m_bSetTimer; }

private:
    // True if a timer event is currently pending.
    bool m_bSetTimer;

    // HWND owned by the thread that CefDoMessageLoopWork should be invoked on.
    HWND m_wndMsgLoop;

    bool m_bRunCefMsgLoop;
    bool m_bReentrancy;
};

#endif // QCEFWINMSGLOOP_H
